package com.ti.shangou.controller.pages.back.role;

import com.ti.shangou.dao.RoleDao;
import com.ti.shangou.pojo.dto.PageDTO;
import com.ti.shangou.pojo.dto.ResponseDTO;
import com.ti.shangou.pojo.entity.Role;
import com.ti.shangou.pojo.query.PageQuery;
import com.ti.shangou.pojo.vo.PermissionVO;
import com.ti.shangou.pojo.vo.RoleVO;
import com.ti.shangou.service.impl.PermissionServiceImpl;
import com.ti.shangou.service.impl.RoleServiceImpl;
import com.ti.shangou.util.Permission.SetPermission;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 作者：田宝山
 * 日期：2020/5/30
 */
@Controller
@RequestMapping("/pages/back/role")
public class RoleController  {
    @Resource
    RoleDao roleDao;
    @Resource
    RoleServiceImpl roleService;
    @Resource
    PermissionServiceImpl permissionService;

    @RequestMapping("list")
    public String toList(){
        return "/pages/back/role/role-list";
    }

    @ResponseBody
    @RequestMapping("ajaxList")
    public Object roleList(Integer page,Integer limit){
        PageQuery pageQuery = new PageQuery(page,limit);
        // 分页查询
    List<RoleVO> roleVOS = roleService.selectRoleList(pageQuery);
        Integer roleCount = roleService.selectRoleCount();

        // 获得权限集合
        List<PermissionVO> permissionVOS = permissionService.selectPermissionsByRoles(roleVOS);
        // 设置权限集合
        List<RoleVO> roleVOList = SetPermission.setPermissionsOfRoles(roleVOS, permissionVOS);

        return new PageDTO().setPageData(roleCount,roleVOList);
    }

    @RequestMapping("add")
    @ResponseBody
    public Object add(Role role){
        return roleService.add(role);
    }

    @RequestMapping("deleteRoles")
    @ResponseBody
    public Object deleteRoles(@RequestBody List<RoleVO> roles){
        return ResponseDTO.get(roleService.deleteRoleByList(roles));
    }

    @ResponseBody
    @RequestMapping("roleChag")
    public Object roleChange( RoleVO roleVO){
        int i = roleService.updateRoleById(roleVO);
        return ResponseDTO.ok("修改成功");
    }

    @ResponseBody
    @RequestMapping("chsys")
    public Object changeSystem(Role role){
        return ResponseDTO.get(roleDao.updateByPrimaryKeySelective(role)==1);
    }

    @RequestMapping("getSystemRoles")
    @ResponseBody
    public Object getSys(){
        List<RoleVO> systemRoles = roleService.getSystemRoles();
        return PageDTO.setPageData(systemRoles.size(),systemRoles);
    }
}
